﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys.Entity
{
    public class User : IEntity
    {
        Crypto m_crypto = new Crypto();

        public int ID = 0;
        public string CODE = "D00000";
        public string NAME = "";
        public string PASSWORD = "";

        public bool FORM_USER_ACCESS = false;
        public bool FORM_USER_SAVE = false;
        public bool FORM_USER_DELETE = false;

        public bool FORM_TT_ACCESS = false;
        public bool FORM_TT_SAVE = false;
        public bool FORM_TT_DELETE = false;

        public bool FORM_CCY_ACCESS = false;
        public bool FORM_CCY_SAVE = false;
        public bool FORM_CCY_DELETE = false;

        public bool FORM_DT_ACCESS = false;
        public bool FORM_DT_SAVE = false;
        public bool FORM_DT_DELETE = false;

        public bool FORM_OWNER_ACCESS = false;
        public bool FORM_OWNER_SAVE = false;
        public bool FORM_OWNER_DELETE = false;

        public bool FORM_VESSEL_ACCESS = false;
        public bool FORM_VESSEL_SAVE = false;
        public bool FORM_VESSEL_DELETE = false;

        public bool FORM_TOP_ACCESS = false;
        public bool FORM_TOP_SAVE = false;
        public bool FORM_TOP_DELETE = false;

        public bool FORM_DRTR_ACCESS = false;
        public bool FORM_DRTR_SAVE = false;
        public bool FORM_DRTR_DELETE = false;

        public bool FORM_CRTR_ACCESS = false;
        public bool FORM_CRTR_SAVE = false;
        public bool FORM_CRTR_DELETE = false;

        public bool FORM_DRTRRPT_ACCESS = false;
        public bool FORM_CRTRRPT_ACCESS = false;

        public User()
        {
        }
        public User(int id)
        {
            ID = id;
        }
        public User(int id, string code)
        {
            ID = id;
            CODE = code;
        }
        public IEntity Get(MySqlDataReader aReader)
        {
            User user = null;
            while (aReader.Read())
            {
                user = new User();
                user.ID = Convert.ToInt32(aReader[0]);
                user.CODE = aReader[1].ToString();
                user.NAME = aReader[2].ToString();
                user.PASSWORD = m_crypto.Decrypt(aReader[3].ToString());
                user.FORM_USER_ACCESS = Convert.ToBoolean(aReader[4]);
                user.FORM_USER_SAVE = Convert.ToBoolean(aReader[5]);
                user.FORM_USER_DELETE = Convert.ToBoolean(aReader[6]);
                user.FORM_TT_ACCESS = Convert.ToBoolean(aReader[7]);
                user.FORM_TT_SAVE = Convert.ToBoolean(aReader[8]);
                user.FORM_TT_DELETE = Convert.ToBoolean(aReader[9]);
                user.FORM_CCY_ACCESS = Convert.ToBoolean(aReader[10]);
                user.FORM_CCY_SAVE = Convert.ToBoolean(aReader[11]);
                user.FORM_CCY_DELETE = Convert.ToBoolean(aReader[12]);

                user.FORM_DT_ACCESS = Convert.ToBoolean(aReader[13]);
                user.FORM_DT_SAVE = Convert.ToBoolean(aReader[14]);
                user.FORM_DT_DELETE = Convert.ToBoolean(aReader[15]);

                user.FORM_DRTR_ACCESS = Convert.ToBoolean(aReader[16]);
                user.FORM_DRTR_SAVE = Convert.ToBoolean(aReader[17]);
                user.FORM_DRTR_DELETE = Convert.ToBoolean(aReader[18]);

                user.FORM_CRTR_ACCESS = Convert.ToBoolean(aReader[19]);
                user.FORM_CRTR_SAVE = Convert.ToBoolean(aReader[20]);
                user.FORM_CRTR_DELETE = Convert.ToBoolean(aReader[21]);

                user.FORM_DRTRRPT_ACCESS = Convert.ToBoolean(aReader[22]);
                user.FORM_CRTRRPT_ACCESS = Convert.ToBoolean(aReader[23]);

                user.FORM_OWNER_ACCESS = Convert.ToBoolean(aReader[24]);
                user.FORM_OWNER_SAVE = Convert.ToBoolean(aReader[25]);
                user.FORM_OWNER_DELETE = Convert.ToBoolean(aReader[26]);

                user.FORM_VESSEL_ACCESS = Convert.ToBoolean(aReader[27]);
                user.FORM_VESSEL_SAVE = Convert.ToBoolean(aReader[28]);
                user.FORM_VESSEL_DELETE = Convert.ToBoolean(aReader[29]);

                user.FORM_TOP_ACCESS = Convert.ToBoolean(aReader[30]);
                user.FORM_TOP_SAVE = Convert.ToBoolean(aReader[31]);
                user.FORM_TOP_DELETE = Convert.ToBoolean(aReader[32]);

            }
            aReader.Close();
            return user;
        }
        public string GetInsertSQL()
        {
            return String.Format(@"insert into table_user 
                (
user_code,user_name, user_password, 
user_f_user_a, user_f_user_s,user_f_user_d, 
user_f_tt_a, user_f_tt_s,user_f_tt_d,
user_f_ccy_a, user_f_ccy_s,user_f_ccy_d,
user_f_dt_a, user_f_dt_s,user_f_dt_d,
user_f_drtr_a, user_f_drtr_s,user_f_drtr_d,
user_f_crtr_a, user_f_crtr_s,user_f_crtr_d,
user_f_drtrrpt_a, user_f_crtrrpt_a,
user_f_owner_a, user_f_owner_s,user_f_owner_d,
user_f_vessel_a, user_f_vessel_s,user_f_vessel_d,
user_f_top_a, user_f_top_s,user_f_top_d
) 
                VALUES ('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}
,{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31})",
                CODE, NAME, m_crypto.Encrypt(PASSWORD), 
                FORM_USER_ACCESS, FORM_USER_SAVE, FORM_USER_DELETE,
                FORM_TT_ACCESS, FORM_TT_SAVE, FORM_TT_DELETE, 
                FORM_CCY_ACCESS, FORM_CCY_SAVE, FORM_CCY_DELETE,
                FORM_DT_ACCESS, FORM_DT_SAVE, FORM_DT_DELETE,
                FORM_DRTR_ACCESS, FORM_DRTR_SAVE, FORM_DRTR_DELETE,
                FORM_CRTR_ACCESS, FORM_CRTR_SAVE, FORM_CRTR_DELETE,
                FORM_DRTRRPT_ACCESS, FORM_CRTRRPT_ACCESS,
                FORM_OWNER_ACCESS, FORM_OWNER_SAVE, FORM_OWNER_DELETE,
                FORM_VESSEL_ACCESS, FORM_VESSEL_SAVE, FORM_VESSEL_DELETE,
                FORM_TOP_ACCESS, FORM_TOP_SAVE, FORM_TOP_DELETE
                );
        }
        public string GetDeleteSQL()
        {
            return "delete from table_user where user_id = " + ID;
        }
        public string GetUpdateSQL()
        {
            return String.Format(@"update table_user set 
                user_code = '{0}', 
                user_name='{1}',
                user_password='{2}',
                user_f_user_a = {3}, 
                user_f_user_s= {4},
                user_f_user_d= {5},
                user_f_tt_a = {6}, 
                user_f_tt_s= {7},
                user_f_tt_d= {8},
                user_f_ccy_a = {9}, 
                user_f_ccy_s= {10},
                user_f_ccy_d= {11},
                user_f_dt_a = {12}, 
                user_f_dt_s= {13},
                user_f_dt_d= {14},
                user_f_drtr_a= {15}, 
                user_f_drtr_s= {16},
                user_f_drtr_d= {17},
                user_f_crtr_a= {18}, 
                user_f_crtr_s= {19},
                user_f_crtr_d= {20},
                user_f_drtrrpt_a= {21}, 
                user_f_crtrrpt_a= {22},
                user_f_owner_a= {23}, 
                user_f_owner_s= {24},
                user_f_owner_d= {25},
                user_f_vessel_a= {26}, 
                user_f_vessel_s= {27},
                user_f_vessel_d= {28},
                user_f_top_a= {29}, 
                user_f_top_s= {30},
                user_f_top_d= {31}
                where user_id = {32}",
                CODE,
                NAME, 
                m_crypto.Encrypt(PASSWORD), 
                FORM_USER_ACCESS, 
                FORM_USER_SAVE, 
                FORM_USER_DELETE,
                FORM_TT_ACCESS, 
                FORM_TT_SAVE, 
                FORM_TT_DELETE, 
                FORM_CCY_ACCESS, 
                FORM_CCY_SAVE, 
                FORM_CCY_DELETE,
                FORM_DT_ACCESS, 
                FORM_DT_SAVE, 
                FORM_DT_DELETE,
                FORM_DRTR_ACCESS, 
                FORM_DRTR_SAVE, 
                FORM_DRTR_DELETE,
                FORM_CRTR_ACCESS, 
                FORM_CRTR_SAVE, 
                FORM_CRTR_DELETE,
                FORM_DRTRRPT_ACCESS, 
                FORM_CRTRRPT_ACCESS,
                FORM_OWNER_ACCESS, 
                FORM_OWNER_SAVE, 
                FORM_OWNER_DELETE,
                FORM_VESSEL_ACCESS, 
                FORM_VESSEL_SAVE, 
                FORM_VESSEL_DELETE,
                FORM_TOP_ACCESS, 
                FORM_TOP_SAVE, 
                FORM_TOP_DELETE,
                ID);
        }
        public string GetByIDSQL(int ID)
        {
            return String.Format("select * from table_user where user_id = {0}", ID);
        }
        public string GetByCodeSQL(string code)
        {
            return String.Format("select * from table_user where user_code = '{0}'", code);
        }
        public string GetByCodeLikeSQL(string text)
        {
            return String.Format("select * from table_user where user_code like '%{0}%'", text);
        }
        public string GetByNameLikeSQL(string text)
        {
            return String.Format("select * from table_user where user_name like '%{0}%'", text);
        }
        public string GetAllSQL()
        {
            return String.Format("select * from table_user");
        }
        public IList GetAll(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                User user = new User();
                user.ID = Convert.ToInt32(aReader[0]);
                user.CODE = aReader[1].ToString();
                user.NAME = aReader[2].ToString();
                user.PASSWORD = m_crypto.Decrypt(aReader[3].ToString());
                user.FORM_USER_ACCESS = Convert.ToBoolean(aReader[4]);
                user.FORM_USER_SAVE = Convert.ToBoolean(aReader[5]);
                user.FORM_USER_DELETE = Convert.ToBoolean(aReader[6]);
                user.FORM_TT_ACCESS = Convert.ToBoolean(aReader[7]);
                user.FORM_TT_SAVE = Convert.ToBoolean(aReader[8]);
                user.FORM_TT_DELETE = Convert.ToBoolean(aReader[9]);
                user.FORM_CCY_ACCESS = Convert.ToBoolean(aReader[10]);
                user.FORM_CCY_SAVE = Convert.ToBoolean(aReader[11]);
                user.FORM_CCY_DELETE = Convert.ToBoolean(aReader[12]);
                user.FORM_DT_ACCESS = Convert.ToBoolean(aReader[10]);
                user.FORM_DT_SAVE = Convert.ToBoolean(aReader[11]);
                user.FORM_DT_DELETE = Convert.ToBoolean(aReader[12]);
                user.FORM_DRTR_ACCESS = Convert.ToBoolean(aReader[16]);
                user.FORM_DRTR_SAVE = Convert.ToBoolean(aReader[17]);
                user.FORM_DRTR_DELETE = Convert.ToBoolean(aReader[18]);

                user.FORM_CRTR_ACCESS = Convert.ToBoolean(aReader[19]);
                user.FORM_CRTR_SAVE = Convert.ToBoolean(aReader[20]);
                user.FORM_CRTR_DELETE = Convert.ToBoolean(aReader[21]);

                user.FORM_DRTRRPT_ACCESS = Convert.ToBoolean(aReader[22]);
                user.FORM_CRTRRPT_ACCESS = Convert.ToBoolean(aReader[23]);

                user.FORM_OWNER_ACCESS = Convert.ToBoolean(aReader[24]);
                user.FORM_OWNER_SAVE = Convert.ToBoolean(aReader[25]);
                user.FORM_OWNER_DELETE = Convert.ToBoolean(aReader[26]);

                user.FORM_VESSEL_ACCESS = Convert.ToBoolean(aReader[27]);
                user.FORM_VESSEL_SAVE = Convert.ToBoolean(aReader[28]);
                user.FORM_VESSEL_DELETE = Convert.ToBoolean(aReader[29]);

                user.FORM_TOP_ACCESS = Convert.ToBoolean(aReader[30]);
                user.FORM_TOP_SAVE = Convert.ToBoolean(aReader[31]);
                user.FORM_TOP_DELETE = Convert.ToBoolean(aReader[32]);
                result.Add(user);
            }
            aReader.Close();
            return result;
        }
        public int GetID()
        {
            return ID;
        }
        public void SetID(int id)
        {
            ID = id;
        }
        public override string ToString()
        {
            return CODE + " - " + NAME;
        }
        public string GetCode()
        {
            return CODE;
        }
        public void SetCode(string code)
        {
            CODE = code;
        }
        public string GetMaximumIDSQL()
        {
            return String.Format("SELECT max(user_id) from table_user");
        }
    }
}
